import requests
from bs4 import BeautifulSoup

baseUrl = 'https://story.beva.com'


def packageTitle(url, title):
    content = requests.get(url=baseUrl + url).content
    soup = BeautifulSoup(content)
    stcontent = soup.select('#stcontent p')
    content = ""
    for c in stcontent:
        if c.string:
            content += c.string
    print(content)
    writeFile(content, title)


def writeFile(content, title):
    f = open("./txt/" + title + ".txt", mode='w')
    f.write(content.encode('UTF-8'))
    f.flush()
    f.close()


# 贝瓦网站故事下载

if __name__ == '__main__':

    for i in range(1, 8):
        shui_qian = '/99/category/shui-qian/' + str(i)
        content = requests.get(url=baseUrl + shui_qian).content
        soup = BeautifulSoup(content)
        # print(soup)
        slist = soup.select('.sll2 a')
        # print(slist)
        for s in slist:
            print(s['title'])
            packageTitle(s['href'], s['title'])
